﻿using IWshRuntimeLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace Angle.Krill.Utils
{
    internal class AutoStartupHelper
    {
        private readonly static object obj = new object();
        private volatile static AutoStartupHelper instance;

        private string systemStartPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
        private string appAllPath = Process.GetCurrentProcess().MainModule.FileName;
        private string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

        private AutoStartupHelper() { }
        /// <summary>
        /// 缓存
        /// </summary>
        public static AutoStartupHelper Instance
        {
            get
            {
                if (null == instance)
                {
                    lock (obj)
                    {
                        instance = new AutoStartupHelper();
                    }
                }

                return instance;
            }
        }

        public string QuikDescribe { get; set; } = Process.GetCurrentProcess().ProcessName;
        public string QuikName { get; set; } = Process.GetCurrentProcess().ProcessName;
        /// <summary>
        /// 设置开机启动
        /// </summary>
        /// <param name="onOff">是否开机启动</param>
        public void SetAutoStartup(bool onOff = true)
        {
            if (onOff)
            {
                List<string> shortcutPaths = this.GetQuickFromFolder(systemStartPath, appAllPath);
                if (shortcutPaths.Count >= 2)
                {
                    foreach (string shortcutPath in shortcutPaths)
                    {
                        this.DeleteFile(shortcutPath);
                    }
                }
                else if (shortcutPaths.Count < 1)
                {
                    this.CreateShortcut(this.systemStartPath, this.QuikName, this.appAllPath, this.QuikDescribe);
                }
            }
            else
            {
                List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath);

                if (shortcutPaths.Any())
                {
                    foreach (string shortcutPath in shortcutPaths)
                    {
                        this.DeleteFile(shortcutPath);
                    }
                }
            }
        }

        public void CreateDesktopQuick(bool isCreate)
        {
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            List<string> shortcutPaths = GetQuickFromFolder(desktopPath, appAllPath);

            if (isCreate)
            {
                if (shortcutPaths.Count < 1)
                {
                    this.CreateShortcut(desktopPath, this.QuikName, this.appAllPath, this.QuikDescribe);
                }
            }
            else
            {
                if (shortcutPaths.Any())
                {
                    foreach (string shortcutPath in shortcutPaths)
                    {
                        this.DeleteFile(shortcutPath);
                    }
                }
            }
        }

        private bool CreateShortcut(string directory, string shortcutName, string targetPath, string description)
        {
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            string shortcutPath = Path.Combine(directory, $"{shortcutName}.lnk");

            if (System.IO.File.Exists(shortcutPath))
            {
                return true;
            }
            WshShell shell = new IWshRuntimeLibrary.WshShell();
            IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);
            shortcut.TargetPath = targetPath;
            shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);
            shortcut.WindowStyle = (int)WshWindowStyle.WshNormalFocus;
            shortcut.Description = description;
            shortcut.Arguments = "auto";
            //shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;
            shortcut.Save();

            return true;
        }

        private void DeleteFile(string path)
        {
            FileAttributes attr = System.IO.File.GetAttributes(path);
            if (attr == FileAttributes.Directory)
            {
                Directory.Delete(path, true);
            }
            else
            {
                System.IO.File.Delete(path);
            }
        }

        private List<string> GetQuickFromFolder(string directory, string targetPath)
        {
            List<string> lstStr = new List<string>();
            string tempStr = string.Empty;
            string[] files = Directory.GetFiles(directory, "*.lnk");
            if (files == null && files.Length <= 0)
            {
                return lstStr;
            }

            foreach (string file in files)
            {
                tempStr = this.GetAppPathFromQuick(file);
                if (tempStr == targetPath)
                {
                    lstStr.Add(file);
                }
            }

            return lstStr;
        }

        private string GetAppPathFromQuick(string shortcutPath)
        {
            if (System.IO.File.Exists(shortcutPath))
            {
                WshShell shell = new WshShell();
                IWshShortcut shortct = (IWshShortcut)shell.CreateShortcut(shortcutPath);

                return shortct.TargetPath;
            }

            return string.Empty;
        }
    }
}
